﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HUGATE.Utility;
using HUGATE.ItemObject;

namespace HUGATE.Item.Controls.ProductAttribute
{
    public partial class ParentAttribute : System.Web.UI.UserControl
    {
        #region Variable
        public DataRow temp;
        //public DataTable dtbl;
        #endregion

        #region Support Function

        /// <summary>
        /// Load data in TreeView
        /// </summary>
        /// 

        private void FillTreeview(TreeNode tnParentdNode, int intCurrentID, DataTable dtbl)
        {
            if (tnParentdNode == null)
            {
                TVparent.Nodes.Clear();
                tnParentdNode = new TreeNode();
                tnParentdNode.Text = "Root";
                tnParentdNode.Value = "0";
                TVparent.Nodes.Add(tnParentdNode);

            }
            DataRow[] objRows = dtbl.Select("ParentAttributeID = " + intCurrentID.ToString());

            foreach (DataRow row in objRows)
            {
                TreeNode child = new TreeNode();
                child.Text = Convert.ToString(row["AttributeName"]);
                child.Value = Convert.ToString(row["AttributeID"]);
                tnParentdNode.ChildNodes.Add(child);
                FillTreeview(child, Convert.ToInt32(row["AttributeID"]), dtbl);
            }
        }

        #endregion

        #region Event Function
        protected void Page_Load(object sender, EventArgs e)
        {
            
                ITEMProductAttribute itemcata = new ITEMProductAttribute();
                DataTable dtbl = itemcata.SearchData("");
                FillTreeview(null, 0, dtbl);
                TVparent.ExpandAll();
        }
        protected void TVparent_SelectedNodeChanged(object sender, EventArgs e)
        {
            ITEMProductAttribute itemcata = new ITEMProductAttribute();
            DataTable dtbl = itemcata.SearchData("");
            if (TVparent.SelectedValue != "0")
            {
                temp = dtbl.Select("AttributeID = " + TVparent.SelectedValue)[0];
            }
            else
            {
                temp = null;
            }
            RaisePostBackGetStringEvent(e);
        }
        #endregion

        #region Event Handler
        public delegate void GetNameParentID(object sender, EventArgs e);
        public event GetNameParentID GetString = null;
        public void RaisePostBackGetStringEvent(EventArgs e)
        {
            if (GetString != null)
                GetString(temp, e);
        }
        #endregion


    }
}